package com.movecar.sortlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.movecar.R;
import com.movecar.model.GroupMemberBean;
import com.movecar.utils.AppConfig;
import com.movecar.utils.BitMapUtil;
import com.movecar.utils.imageloader.Constants;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SortGroupMemberAdapter extends BaseAdapter implements SectionIndexer {
	private List<GroupMemberBean> list = null;
	private Context mContext;
	private ImageLoader imageLoader = null;
	private boolean checkAble = false;
	private List<String> friends = new ArrayList<String>();


	public static Map<Integer, Boolean> isSelected;

	public SortGroupMemberAdapter(Context mContext, List<GroupMemberBean> list) {
		this.mContext = mContext;
		this.list = list;
		imageLoader = ImageLoader.getInstance();

		// 这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false。
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < list.size(); i++) {
			isSelected.put(i, false);
		}
	}

	/**
	 * 当ListView数据发生变化时,调用此方法来更新ListView
	 * 
	 * @param list
	 */
	public void updateListView(List<GroupMemberBean> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public int getCount() {
		return this.list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View view, ViewGroup arg2) {
		ViewHolder viewHolder = null;
		final GroupMemberBean mContent = list.get(position);
		if (view == null) {
			viewHolder = new ViewHolder();
			view = LayoutInflater.from(mContext).inflate(R.layout.activity_group_member_item, null);
			viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
			viewHolder.line_catalog = (TextView) view.findViewById(R.id.line_catalog);
			viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
			viewHolder.user_img = (ImageView) view.findViewById(R.id.user_img);
			viewHolder.order_choose = (CheckBox) view.findViewById(R.id.order_choose);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		// 根据position获取分类的首字母的Char ascii值
		int section = getSectionForPosition(position);
		if (checkAble) {
			viewHolder.order_choose.setVisibility(View.VISIBLE);
			viewHolder.order_choose.setChecked(isSelected.get(position));

		}
		// 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
		if (position == getPositionForSection(section)) {

			viewHolder.tvLetter.setVisibility(View.VISIBLE);
			viewHolder.line_catalog.setVisibility(View.VISIBLE);
			viewHolder.tvLetter.setText(mContent.getSortLetters());
			if (!mContent.getSortLetters().equals("好友申请")) {
				if (null == mContent.getIcon() || "".equals(mContent.getIcon())) {
					viewHolder.user_img.setImageBitmap(BitMapUtil.getRoundedCornerBitmap(BitMapUtil
							.drawableToBitmap(mContext.getResources().getDrawable(R.drawable.default_user_photo))));
				} else {
					if (mContent.getIcon().contains("default")) {
						imageLoader.displayImage(AppConfig.HOST + mContent.getIcon(), viewHolder.user_img,
								Constants.image_display_options);
					} else {
						imageLoader.displayImage(AppConfig.HOST + mContent.getIcon(), viewHolder.user_img,
								Constants.image_display_options_conner);
					}
				}

			} else {
				imageLoader.displayImage(AppConfig.HOST + mContent.getIcon(), viewHolder.user_img,
						Constants.image_display_options);
			}

		} else {
			if (null == mContent.getIcon() || "".equals(mContent.getIcon())) {
				viewHolder.user_img.setImageBitmap(BitMapUtil.getRoundedCornerBitmap(BitMapUtil
						.drawableToBitmap(mContext.getResources().getDrawable(R.drawable.default_user_photo))));
			} else {
				if (mContent.getIcon().contains("default")) {
					imageLoader.displayImage(AppConfig.HOST + mContent.getIcon(), viewHolder.user_img,
							Constants.image_display_options);
				} else {
					imageLoader.displayImage(AppConfig.HOST + mContent.getIcon(), viewHolder.user_img,
							Constants.image_display_options_conner);
				}

			}
			viewHolder.tvLetter.setVisibility(View.GONE);
			viewHolder.line_catalog.setVisibility(View.GONE);
		}

		viewHolder.tvTitle.setText(this.list.get(position).getName());
		
		viewHolder.order_choose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (isSelected.get(position)) {
					SortGroupMemberAdapter.isSelected.put(position, false);
					friends.remove(mContent.getFriendId());
					
				} else {
					friends.add(mContent.getFriendId());
					SortGroupMemberAdapter.isSelected.put(position, true);
				}
			}
		});

		return view;

	}

	public class ViewHolder {
		public TextView tvLetter, line_catalog;
		public TextView tvTitle;
		public ImageView user_img;
		public CheckBox order_choose;
	}

	/**
	 * 根据ListView的当前位置获取分类的首字母的Char ascii值
	 */
	public int getSectionForPosition(int position) {
		return list.get(position).getSortLetters().charAt(0);
	}

	/**
	 * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
	 */
	public int getPositionForSection(int section) {
		for (int i = 0; i < getCount(); i++) {
			String sortStr = list.get(i).getSortLetters();
			char firstChar = sortStr.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}

		return -1;
	}

	/**
	 * 提取英文的首字母，非英文字母用#代替。
	 * 
	 * @param str
	 * @return
	 */
	private String getAlpha(String str) {
		String sortStr = str.trim().substring(0, 1).toUpperCase();
		// 正则表达式，判断首字母是否是英文字母
		if (sortStr.matches("[A-Z]")) {
			return sortStr;
		} else {
			return "#";
		}
	}

	@Override
	public Object[] getSections() {
		return null;
	}

	public void setCheckAble(boolean checkAble) {
		this.checkAble = checkAble;
	}

	public List<String> getFriends() {
		return friends;
	}
}